package com.lt.flashcard.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.util.Clients;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Notification;
import com.lt.flashcard.model.service.MessageService;
import com.lt.flashcard.model.service.NotificationService;
import com.lt.flashcard.model.service.Repository;
import com.lt.flashcard.model.service.UserService;

public class NotificationCtrl {

    private List<Object[]> nots = null;
    private SimpleDateFormat fm = new SimpleDateFormat("yyyyMMdd");
    private Long userid;

    @Init
    public void init() {
        if (!Utility.isLogin()) {
            Utility.loginOrAccessDenied(false);
            return;
        }

        loadList();

        Date n = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(n);
        c.add(Calendar.DATE, -1);

        now = fm.format(n);
        yesterday = fm.format(c.getTime());
        userid = Utility.getUser().getId();
    }

    /**
     * Get the template name for each notification
     * @param not
     * @return
     */
    public String getTemplateName(int type) {
        switch (type) {
        case Notification.TYPE_SYSTEM:
            return "system";
        case Notification.TYPE_COMMENT:
            return "comment";
        case Notification.TYPE_FRIENDREQUEST:
            return "friendrequest";
        case Notification.TYPE_PRIVATEMSG:
            return "privatemsg";
        case Notification.TYPE_RATING:
            return "rating";
        case Notification.TYPE_SHARE:
            return "share";
        case Notification.TYPE_REPLYCOMMENT:
            return "replycomment";
        case Notification.TYPE_ACCEPTEDFRIENDREQUEST:
            return "acceptfriendrequest";
        case Notification.TYPE_GETMEDAL:
            return "getmedal";
        }
        return "unknow";
    }

    private String now = "";
    private String yesterday = "";
    private String previous = "";

    public void loadList() {
        NotificationService srv = Repository.getInstance().getNotificationService();
        List<Notification> ls = srv.getAllNotificationOfUser(Utility.getUser().getId());

        List<Notification> currentList = new ArrayList<Notification>();
        nots = new ArrayList<Object[]>();

        for (Notification not : ls) {
            String d = fm.format(not.getCreated());
            if (d.equals(previous)) {
                currentList.add(not);
            } else {
                currentList = new ArrayList<Notification>();
                currentList.add(not);
                if (d.equals(now)) {
                    nots.add(new Object[] { "Hôm nay (".concat(Utility.formatDate(not.getCreated())).concat(")"), currentList });
                } else if (d.equals(yesterday)) {
                    nots.add(new Object[] { "Hôm qua (".concat(Utility.formatDate(not.getCreated())).concat(")"), currentList });
                } else {
                    nots.add(new Object[] { Utility.formatDate(not.getCreated()), currentList });
                }
                previous = d;
            }
        }
    }

    @Command
    public void addfriend(@BindingParam("notid") String notid) {
        Notification not = findById(notid);
        if (not != null) {
            UserService usrv = Repository.getInstance().getUserService();
            usrv.addFriendShip(not.getSourceuid(), not.getTargetuid());
            Utility.redirect("", true);
        } else {
            Clients.evalJavaScript("showNotice('', 'Unknow notification')");
        }
    }

    private Notification findById(String id) {
        for (Object[] obj : nots) {
            List<Notification> lst = (List<Notification>) obj[1];
            for (Notification not : lst) {
                if (not.getId().toString().equals(id)) {
                    return not;
                }
            }
        }
        return null;
    }

    /**
     * Count number of new message
     * @return
     */
    public long getCountNewMessage() {
        MessageService msrv = Repository.getInstance().getMessageService();
        return msrv.countUnreadMessage(userid);
    }

    public List<Object[]> getNots() {
        return nots;
    }

    public void setNots(List<Object[]> nots) {
        this.nots = nots;
    }
}
